package com.smartleanx.module.hrm.service.dingDing;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.annotation.InterceptorIgnore;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.smartleanx.framework.common.pojo.CommonResult;
import com.smartleanx.framework.datapermission.core.annotation.DataPermission;
import com.smartleanx.framework.mybatis.core.query.LambdaQueryWrapperX;
import com.smartleanx.framework.security.core.util.SecurityFrameworkUtils;
import com.smartleanx.framework.tenant.core.aop.TenantIgnore;
import com.smartleanx.framework.web.core.util.WebFrameworkUtils;
import com.smartleanx.module.hrm.controller.admin.dingDing.bo.DingAttendanceConfirmationBo;
import com.smartleanx.module.hrm.controller.admin.dingDing.bo.DingAttendanceRecordBo;
import com.smartleanx.module.hrm.controller.admin.dingDing.bo.DingConfirmationBo;
import com.smartleanx.module.hrm.controller.admin.dingDing.vo.DingAttendanceConfirmationVo;
import com.smartleanx.module.hrm.controller.admin.dingDing.vo.DingAttendanceRecordVo;
import com.smartleanx.module.hrm.controller.admin.dingDing.vo.SystemFrameVo;
import com.smartleanx.module.hrm.dal.dataobject.dingDing.*;
import com.smartleanx.module.hrm.dal.dataobject.employee.EmployeeDO;
import com.smartleanx.module.hrm.dal.dataobject.salary.SystemColumnTenant;
import com.smartleanx.module.hrm.dal.dataobject.salary.SystemColumnValue;
import com.smartleanx.module.hrm.dal.mysql.dingDing.*;
import com.smartleanx.module.hrm.dal.mysql.employee.EmployeeMapper;
import com.smartleanx.module.hrm.dal.mysql.salary.SystemColumnTenantMapper;
import com.smartleanx.module.hrm.dal.mysql.salary.SystemColumnValueMapper;
import com.smartleanx.module.hrm.enums.ErrorCodeConstants;
import com.smartleanx.module.hrm.service.examine.util.DateUtil;
import com.smartleanx.module.hrm.service.examine.util.exception.ResultException;
import com.smartleanx.module.system.api.dept.DeptApi;
import com.smartleanx.module.system.api.dept.PostApi;
import com.smartleanx.module.system.api.dept.dto.DeptRespDTO;
import com.smartleanx.module.system.api.dept.dto.PostRespDTO;
import javafx.scene.input.DataFormat;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.validation.annotation.Validated;

import javax.annotation.security.PermitAll;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static com.smartleanx.framework.common.exception.util.ServiceExceptionUtil.exception;


@Service
@Validated
public class DingUserServiceImpl implements DingUserService {
    @Autowired
    private DingUserMapper userMapper;
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private DingDeptMapper dingDeptMapper;
    @Autowired
    private DingDeptUserMapper dingDeptUserMapper;
    @Autowired
    private DingAttendanceRecordMapper dingAttendanceRecordMapper;
    @Autowired
    private DeptApi deptApi;
    @Autowired
    private PostApi postApi;
    @Autowired
    private DingColumnMapper dingColumnMapper;
    @Autowired
    private DingColumnValueMapper dingColumnValueMapper;
    @Autowired
    private DingAttendanceTeamMapper dingAttendanceTeamMapper;
    @Autowired
    private DingUserMapper dingUserMapper;
    @Autowired
    private DingAttendanceConfirmationMapper dingAttendanceConfirmationMapper;
    @Autowired
    private DingConfirmationMapper dingConfirmationMapper;
    @Autowired
    private ConfirmationColumnMapper confirmationColumnMapper;
    @Autowired
    private DingTenantMapper dingTenantMapper;
    @Autowired
    private SystemColumnValueMapper systemColumnValueMapper;
    @Autowired
    private SystemColumnTenantMapper systemColumnTenantMapper;
    @Override
    public List<JSONObject> geBySelect(String dingUserName) {
        List<JSONObject> jsonObjectList = new ArrayList<>();
        List<DingUser> dingUsers = userMapper.selectList(
                new LambdaQueryWrapperX<DingUser>() {{
                    like(!ObjectUtil.isEmpty(dingUserName), DingUser::getName, dingUserName);
                }}
        );
        if (dingUsers == null || dingUsers.isEmpty()) {
            return jsonObjectList;
        } else {
            dingUsers.forEach(dingUser -> {
                JSONObject result = new JSONObject();
                result.put("id", dingUser.getId());
                result.put("value", dingUser.getName());
                jsonObjectList.add(result);
            });
        }
        return jsonObjectList;

    }

    @Override
    public Integer bdDingUser(Integer employeeId, Integer dingUserId) {
        EmployeeDO employeeDO = employeeMapper.selectById(employeeId);
        DingUser dingUser = userMapper.selectById(dingUserId);
        employeeDO.setDingUserId(dingUser.getId());
        employeeMapper.updateById(employeeDO);
        return 1;
    }

    @Override
    public Integer removeBdDingUser(Integer employeeId) {
        EmployeeDO employeeDO = employeeMapper.selectById(employeeId);
        employeeDO.setDingUserId(null);
        employeeMapper.updateById(employeeDO);
        return 0;
    }

    @Override
    public SystemFrameVo getFrameByDepartmentId(Integer departmentId, boolean getChild) {
        SystemFrameVo item = new SystemFrameVo();
        DingDept dingDept = dingDeptMapper.selectById(departmentId);
        if (ObjectUtil.isEmpty(dingDept)) {
            throw exception(ErrorCodeConstants.DING_DING_NOT_CROP);
        }
        if (departmentId == 0) {
        } else {
            item.setId(departmentId);
            item.setName(dingDept.getName());
            item.setType(1);
            item.setIcon("el-icon-folder");
            List<SystemFrameVo> employees = dingDeptMapper.selectEmployeesByDepartmentId(departmentId);
            item.setHasChild(true);
            item.getChildren().addAll(employees);
        }
        // 查询子部门
        if (getChild) {
            List<DingDept> departments = dingDept.selectList(new LambdaQueryWrapper<DingDept>() {
                {
                    eq(DingDept::getParentId, departmentId);
                }
            });
            if (departments != null && departments.size() > 0) {
                for (DingDept department : departments) {
                    SystemFrameVo child = getFrameByDepartmentId(department.getId(), getChild);
                    item.getChildren().add(child);
                }
            }
        }
        return item;
    }

    @Override
    public PageInfo selectList(DingAttendanceRecordBo dingAttendanceRecordBo) {
        if (!ObjectUtil.isEmpty(dingAttendanceRecordBo)) {
            switch (dingAttendanceRecordBo.getType()) {
                case 1:
                    dingAttendanceRecordBo.setEmployeeIdList(new ArrayList<>());
                    break;
                case 2:
                    break;
                case 3:
                    break;
                case 4:
                    break;
            }
        }

        PageHelper.startPage(dingAttendanceRecordBo.getPageNum(), dingAttendanceRecordBo.getPageSize());
        if (!ObjectUtil.isEmpty(dingAttendanceRecordBo.getStartTime()) && !ObjectUtil.isEmpty(dingAttendanceRecordBo.getEndTime())) {
            Long startTime = DateUtil.stringToDate(dingAttendanceRecordBo.getStartTime()).getTime();
            Long endTime = DateUtil.stringToDate(dingAttendanceRecordBo.getEndTime()).getTime();
            dingAttendanceRecordBo.setStartTimeL(startTime);
            dingAttendanceRecordBo.setEndTimeL(endTime);
        }

        List<DingAttendanceRecordVo> dingAttendanceRecordVos = dingAttendanceRecordMapper.selectRecordList(dingAttendanceRecordBo);
        dingAttendanceRecordVos.forEach(dingAttendanceRecordVo -> {
            CommonResult<DeptRespDTO> dept = deptApi.getDept(Long.parseLong(dingAttendanceRecordVo.getDepartmentName()));
            CommonResult<PostRespDTO> post = postApi.getPost(Long.parseLong(dingAttendanceRecordVo.getPostName()));
            dept.checkError();
            String name = dept.getData().getName();
            dingAttendanceRecordVo.setDepartmentName(name);
            post.checkError();
            dingAttendanceRecordVo.setPostName(post.getData().getName());
            switch (dingAttendanceRecordVo.getInvalidRecordType()) {
                case "1":
                    dingAttendanceRecordVo.setInvalidRecordType("安全相关原因");
                    break;
                case "2":
                    dingAttendanceRecordVo.setInvalidRecordType("其他原因");
                    break;
                case "-1":
                    dingAttendanceRecordVo.setInvalidRecordType("无");
                    break;
            }
            switch (dingAttendanceRecordVo.getTimeResult()) {
                case "-1":
                    dingAttendanceRecordVo.setTimeResult("打卡无效:已经打过卡了");
                    break;
                case "1":
                    dingAttendanceRecordVo.setTimeResult("正常");
                    break;
                case "2":
                    dingAttendanceRecordVo.setTimeResult("早退");
                    break;
                case "3":
                    dingAttendanceRecordVo.setTimeResult("迟到");
                    break;
                case "4":
                    dingAttendanceRecordVo.setTimeResult("严重迟到");
                    break;
                case "5":
                    dingAttendanceRecordVo.setTimeResult("旷工迟到");
                    break;
                case "6":
                    dingAttendanceRecordVo.setTimeResult("未打卡");
                    break;
            }
            if (!ObjectUtil.isEmpty(dingAttendanceRecordVo.getWordDate())) {
                dingAttendanceRecordVo.setWordDate(DateUtil.czString(Long.parseLong(dingAttendanceRecordVo.getWordDate())));

            }
            if (!ObjectUtil.isEmpty(dingAttendanceRecordVo.getKqTime())) {
                dingAttendanceRecordVo.setKqTime(DateUtil.czString(Long.parseLong(dingAttendanceRecordVo.getKqTime())));

            }
            if (!ObjectUtil.isEmpty(dingAttendanceRecordVo.getDkTime())) {
                dingAttendanceRecordVo.setDkTime(DateUtil.czString(Long.parseLong(dingAttendanceRecordVo.getDkTime())));

            }
            if (!ObjectUtil.isEmpty(dingAttendanceRecordVo.getPhotoUrl())) {
                List<String> collect = Arrays.stream(dingAttendanceRecordVo.getPhotoUrl().split(",")).collect(Collectors.toList());
                dingAttendanceRecordVo.setPhotoUrlList(collect);
            }
        });
        return new PageInfo<>(dingAttendanceRecordVos);
    }


    @Override
    public List<JSONObject> selectTeamList(String teamName) {
        List<JSONObject> jsonObjectList = dingAttendanceRecordMapper.selectTeamList(teamName);
        return jsonObjectList;
    }


    @Override
    public JSONObject selectColumnList(DingAttendanceRecordBo dingAttendanceRecordBo) {
        if (!ObjectUtil.isEmpty(dingAttendanceRecordBo)) {
            switch (dingAttendanceRecordBo.getType()) {
                case 1:
                    dingAttendanceRecordBo.setEmployeeIdList(new ArrayList<>());
                    break;
                case 2:
                    break;
                case 3:
                    break;
                case 4:
                    break;
            }
        }
        JSONObject result = new JSONObject();
        List<JSONObject> columnObjectList = new ArrayList<>();
        List<DingColumn> dingColumns = dingColumnMapper.selectList(new LambdaQueryWrapper<DingColumn>() {{
            eq(DingColumn::getStatus, 0);
        }});
        JSONObject columnObject = new JSONObject();
        columnObject.put("id", 9991);
        columnObject.put("name", "姓名");
        columnObject.put("type", 0);
        columnObjectList.add(columnObject);
        columnObject = new JSONObject();
        columnObject.put("id", 9992);
        columnObject.put("name", "考勤组");
        columnObject.put("type", 0);
        columnObjectList.add(columnObject);
        columnObject = new JSONObject();
        columnObject.put("id", 9993);
        columnObject.put("name", "部门");
        columnObject.put("type", 0);
        columnObjectList.add(columnObject);
        columnObject = new JSONObject();
        columnObject.put("id", 9994);
        columnObject.put("name", "工号");
        columnObject.put("type", 0);
        columnObjectList.add(columnObject);
        columnObject = new JSONObject();
        columnObject.put("id", 9995);
        columnObject.put("name", "职位");
        columnObject.put("type", 0);
        columnObjectList.add(columnObject);
        for (int i = 0; i < dingColumns.size(); i++) {
            columnObject = new JSONObject();
            DingColumn dingColumn = dingColumns.get(i);
            if (dingColumn.getId() == 1 || dingColumn.getId() == 3 || dingColumn.getId() == 5 || dingColumn.getId() == 7 || dingColumn.getId() == 9 || dingColumn.getId() == 11 || dingColumn.getId() == 20 || dingColumn.getId() == 22) {
                columnObject.put("type", 1);
            } else {
                columnObject.put("type", 0);
            }
            columnObject.put("id", dingColumn.getId());
            columnObject.put("name", dingColumn.getName());
            columnObjectList.add(columnObject);
        }
        List<EmployeeDO> employeeDOS = employeeMapper.selectEmployeeList(dingAttendanceRecordBo);
        List<JSONObject> valuesObject = new ArrayList<>();
        for (int e = 0; e < employeeDOS.size(); e++) {
            EmployeeDO employeeDO = employeeDOS.get(e);
            List<DingColumnValue> dingColumnValues = dingColumnValueMapper.selectList(new LambdaQueryWrapper<DingColumnValue>() {{
                eq(DingColumnValue::getDingUserId, employeeDO.getDingUserId());
                le(DingColumnValue::getDate, dingAttendanceRecordBo.getEndTime() + " 00:00:00");
                ge(DingColumnValue::getDate, dingAttendanceRecordBo.getStartTime() + " 00:00:00");
            }});
            JSONObject columnValueObject = new JSONObject();
            columnValueObject.put("9991", employeeDO.getName());
            DingUser dingUser = dingUserMapper.selectById(employeeDO.getDingUserId());
            if (!ObjectUtil.isEmpty(dingUser)) {
                DingAttendanceTeam dingAttendanceTeam = dingAttendanceTeamMapper.selectById(dingUser.getTeamId());
                if (!ObjectUtil.isEmpty(dingAttendanceTeam)) {
                    columnValueObject.put("9992", dingAttendanceTeam.getName());
                }
            }
            CommonResult<DeptRespDTO> dept = deptApi.getDept(employeeDO.getDeptId());
            columnValueObject.put("9993", dept.getCheckedData().getName());
            columnValueObject.put("9994", employeeDO.getEmployeeNo());
            CommonResult<PostRespDTO> post = postApi.getPost(employeeDO.getPostId());
            columnValueObject.put("9995", post.getCheckedData().getName());
            for (int i = 0; i < dingColumnValues.size(); i++) {
                DingColumnValue dingColumnValue = dingColumnValues.get(i);
                if (dingColumnValue.getColumnValue().contains(".")) {
                    if (Integer.parseInt(dingColumnValue.getColumnValue().split("\\.")[1]) == 0) {
                        if (Integer.parseInt(dingColumnValue.getColumnValue().split("\\.")[0]) == 0) {
                            dingColumnValue.setColumnValue("");
                        } else {
                            dingColumnValue.setColumnValue(dingColumnValue.getColumnValue().split("\\.")[0]);
                        }
                    }
                }
                if (dingColumnValue.getColumnId() == 1 || dingColumnValue.getColumnId() == 3 || dingColumnValue.getColumnId() == 5 || dingColumnValue.getColumnId() == 7 || dingColumnValue.getColumnId() == 9 || dingColumnValue.getColumnId() == 11 || dingColumnValue.getColumnId() == 20 || dingColumnValue.getColumnId() == 22) {
                    JSONObject object = new JSONObject();
                    object.put("id", dingColumnValue.getId());
                    if (dingColumnValue.getColumnValue().equals("正常")) {
                        object.put("type", 1);
                    } else {
                        object.put("type", 2);
                    }
                    object.put("status", dingColumnValue.getColumnValue());
                    object.put("time", DateUtil.strZLong(dingColumnValue.getDate()));
                    object.put("userid", dingUser.getUserid());
//                    object.put("status", status);
                    object.put("dingUserId", dingUser.getUserid());
                    columnValueObject.put(dingColumnValue.getColumnId().toString(), object);
                } else {
                    columnValueObject.put(dingColumnValue.getColumnId().toString(), dingColumnValue.getColumnValue());

                }
            }

            valuesObject.add(columnValueObject);
        }
        result.put("columns", columnObjectList);
        result.put("columnsValue", new PageInfo<>(valuesObject));
        return result;
    }


    @Override
    public JSONObject selectColumnMonthList(DingAttendanceRecordBo dingAttendanceRecordBo) {
        if (!ObjectUtil.isEmpty(dingAttendanceRecordBo)) {
            switch (dingAttendanceRecordBo.getType()) {
                case 1:
                    dingAttendanceRecordBo.setEmployeeIdList(new ArrayList<>());
                    break;
                case 2:
                    break;
                case 3:
                    break;
                case 4:
                    break;
            }
        }


        List<String> strDateListBetween = DateUtil.getStrDateListBetween(dingAttendanceRecordBo.getStartTime(), dingAttendanceRecordBo.getEndTime());
        JSONObject result = new JSONObject();
        List<JSONObject> columnObjectList = new ArrayList<>();
        List<DingColumn> dingColumns = dingColumnMapper.selectList(new LambdaQueryWrapper<DingColumn>() {{
            eq(DingColumn::getSubType, 0);
            eq(DingColumn::getStatus, 0);
        }});
        JSONObject columnObject = new JSONObject();
        columnObject.put("id", 9991);
        columnObject.put("name", "姓名");
        columnObject.put("type", 0);
        columnObjectList.add(columnObject);
        columnObject = new JSONObject();
        columnObject.put("id", 9992);
        columnObject.put("name", "考勤组");
        columnObject.put("type", 0);
        columnObjectList.add(columnObject);
        columnObject = new JSONObject();
        columnObject.put("id", 9993);
        columnObject.put("name", "部门");
        columnObject.put("type", 0);
        columnObjectList.add(columnObject);
        columnObject = new JSONObject();
        columnObject.put("id", 9994);
        columnObject.put("name", "工号");
        columnObject.put("type", 0);
        columnObjectList.add(columnObject);
        columnObject = new JSONObject();
        columnObject.put("id", 9995);
        columnObject.put("name", "职位");
        columnObject.put("type", 0);
        columnObjectList.add(columnObject);
        for (int i = 0; i < dingColumns.size(); i++) {
            columnObject = new JSONObject();
            DingColumn dingColumn = dingColumns.get(i);
            columnObject.put("id", dingColumn.getId());
            columnObject.put("name", dingColumn.getName());
            columnObject.put("type", 0);
            columnObjectList.add(columnObject);
        }

        for (int i = 0; i < strDateListBetween.size(); i++) {
            columnObject = new JSONObject();
            columnObject.put("id", 99999 + i);
            columnObject.put("name", strDateListBetween.get(i));
            columnObject.put("type", 1);
            columnObjectList.add(columnObject);
        }
        List<EmployeeDO> employeeDOS = employeeMapper.selectEmployeeList(dingAttendanceRecordBo);
        List<JSONObject> valuesObject = new ArrayList<>();
        for (int e = 0; e < employeeDOS.size(); e++) {
            EmployeeDO employeeDO = employeeDOS.get(e);
            List<DingColumnValue> dingColumnValues = dingColumnValueMapper.selectList(new QueryWrapper<DingColumnValue>() {{
                select("if (sum( column_value ) =0,column_value,sum( column_value )) AS columnValue,\n" +
                        "\tcolumn_id columnId  ");
                eq("ding_user_id", employeeDO.getDingUserId());
                le("date", dingAttendanceRecordBo.getEndTime() + " 23:59:59");
                ge("date", dingAttendanceRecordBo.getStartTime() + " 00:00:00");
                groupBy("ding_user_id,column_id");
                orderByDesc("date");

            }});
            columnObject = new JSONObject();
            columnObject.put("9991", employeeDO.getName());
            DingUser dingUser = dingUserMapper.selectById(employeeDO.getDingUserId());
            if (!ObjectUtil.isEmpty(dingUser)) {
                DingAttendanceTeam dingAttendanceTeam = dingAttendanceTeamMapper.selectById(dingUser.getTeamId());
                if (!ObjectUtil.isEmpty(dingAttendanceTeam)) {
                    columnObject.put("9992", dingAttendanceTeam.getName());
                }
            }
            CommonResult<DeptRespDTO> dept = deptApi.getDept(employeeDO.getDeptId());
            columnObject.put("9993", dept.getCheckedData().getName());
            columnObject.put("9994", employeeDO.getEmployeeNo());
            CommonResult<PostRespDTO> post = postApi.getPost(employeeDO.getPostId());
            columnObject.put("9995", post.getCheckedData().getName());
            for (int i = 0; i < dingColumnValues.size(); i++) {
                DingColumnValue dingColumnValue = dingColumnValues.get(i);
                if (dingColumnValue.getColumnValue().contains(".")) {
                    if (Integer.parseInt(dingColumnValue.getColumnValue().split("\\.")[1]) == 0) {
                        if (Integer.parseInt(dingColumnValue.getColumnValue().split("\\.")[0]) == 0) {
                            dingColumnValue.setColumnValue("");
                        } else {
                            dingColumnValue.setColumnValue(dingColumnValue.getColumnValue().split("\\.")[0]);
                        }
                    }
                }
                columnObject.put(dingColumnValue.getColumnId().toString(), dingColumnValue.getColumnValue());
            }
            for (int i = 0; i < strDateListBetween.size(); i++) {
                Integer dingUserId = employeeDO.getDingUserId();
                DingUser dingUserZ = dingUserMapper.selectById(dingUserId);
                Long l = DateUtil.strZLong(strDateListBetween.get(i));
                DingAttendanceRecord dingAttendanceRecordShang = dingAttendanceRecordMapper.selectOne(new LambdaQueryWrapper<DingAttendanceRecord>() {{
                    eq(DingAttendanceRecord::getUserId, dingUserZ.getUserid());
                    eq(DingAttendanceRecord::getWorkDate, l);
                    eq(DingAttendanceRecord::getCheckType, 1);
                    last("limit 1");
                }});
                DingAttendanceRecord dingAttendanceRecordXia = dingAttendanceRecordMapper.selectOne(new LambdaQueryWrapper<DingAttendanceRecord>() {{
                    eq(DingAttendanceRecord::getUserId, dingUserZ.getUserid());
                    eq(DingAttendanceRecord::getWorkDate, l);
                    eq(DingAttendanceRecord::getCheckType, 2);
                    last("limit 1");
                }});
                List<JSONObject> status = new ArrayList<>();
                if (!ObjectUtil.isEmpty(dingAttendanceRecordShang) && !ObjectUtil.isEmpty(dingAttendanceRecordXia)) {
                    JSONObject obj = new JSONObject();
                    if (dingAttendanceRecordShang.getTimeResult() == 1 && dingAttendanceRecordXia.getTimeResult() == 1) {
                        obj.put("status", "正常");
                        obj.put("type", 1);
                        status.add(obj);
                    } else if (dingAttendanceRecordShang.getTimeResult() == 1) {
                        switch (dingAttendanceRecordXia.getTimeResult()) {
                            case 2:
                                obj.put("status", "下班早退");
                                obj.put("type", 2);
                                status.add(obj);
                                break;
                            case 6:
                                obj.put("status", "下班未打卡");
                                obj.put("type", 2);
                                status.add(obj);
                                break;
                        }
                    } else if (dingAttendanceRecordXia.getTimeResult() == 1) {
                        switch (dingAttendanceRecordShang.getTimeResult()) {
                            case 3:
                                obj.put("status", "迟到");
                                obj.put("type", 2);
                                status.add(obj);
                                break;

                            case 4:
                                obj.put("status", "严重迟到");
                                obj.put("type", 2);
                                status.add(obj);
                                break;
                            case 5:
                                obj.put("status", "旷工迟到");
                                obj.put("type", 2);
                                status.add(obj);
                                break;
                            case 6:
                                obj.put("status", "未打卡");
                                obj.put("type", 2);
                                status.add(obj);
                                break;
                        }
                    } else if (dingAttendanceRecordShang.getTimeResult() == 6 && dingAttendanceRecordXia.getTimeResult() == 6) {
                        obj.put("status", "旷工");
                        obj.put("type", 2);
                        status.add(obj);
                    } else {
                        switch (dingAttendanceRecordXia.getTimeResult()) {
                            case 2:
                                obj.put("status", "下班早退");
                                obj.put("type", 2);
                                status.add(obj);
                                break;

                            case 6:
                                obj.put("status", "下班未打卡");
                                obj.put("type", 2);
                                status.add(obj);
                                break;
                        }
                        switch (dingAttendanceRecordShang.getTimeResult()) {
                            case 3:
                                obj.put("status", "迟到");
                                obj.put("type", 2);
                                status.add(obj);
                                break;

                            case 4:
                                obj.put("status", "严重迟到");
                                obj.put("type", 2);
                                status.add(obj);
                                break;
                            case 5:
                                obj.put("status", "旷工迟到");
                                obj.put("type", 2);
                                status.add(obj);
                                break;
                            case 6:
                                obj.put("status", "上班未打卡");

                                obj.put("type", 2);
                                status.add(obj);
                                break;
                        }
                    }

                }
                JSONObject columnValueObject = new JSONObject();
                columnValueObject.put("time", l);
                columnValueObject.put("userid", dingUserZ.getUserid());
                columnValueObject.put("status", status);
                columnValueObject.put("dingUserId", dingUserZ.getUserid());
                columnObject.put(99999 + i + "", columnValueObject);
            }
            valuesObject.add(columnObject);
        }
        result.put("columns", columnObjectList);
        result.put("columnsValue", new PageInfo<>(valuesObject));
        return result;
    }

    @Override
    public JSONObject monthInfo(Long dingUserId, Long time) {
        DingUser dingUser = dingUserMapper.selectOne(new LambdaQueryWrapper<DingUser>() {{
            eq(DingUser::getUserid, dingUserId);
        }});
        EmployeeDO employeeDO = employeeMapper.selectOne(new LambdaQueryWrapper<EmployeeDO>() {{
            eq(EmployeeDO::getDingUserId, dingUser.getId());
            last("limit 1");
        }});
        List<DingAttendanceRecord> dingAttendanceRecordShang = dingAttendanceRecordMapper.selectList(new LambdaQueryWrapper<DingAttendanceRecord>() {{
            eq(DingAttendanceRecord::getUserId, dingUserId);
            eq(DingAttendanceRecord::getWorkDate, time);
        }});
        JSONObject obj = new JSONObject();
        obj.put("employeeName", employeeDO.getName());
        if (!ObjectUtil.isEmpty(dingAttendanceRecordShang)) {
            Long shangTime = 0L;
            Long xiaTime = 0L;
            List<DingAttendanceRecord> shang = dingAttendanceRecordShang.stream().filter(dingAttendanceRecord -> dingAttendanceRecord.getTimeResult() == 1).collect(Collectors.toList());
            List<DingAttendanceRecord> xia = dingAttendanceRecordShang.stream().filter(dingAttendanceRecord -> dingAttendanceRecord.getTimeResult() == 2).collect(Collectors.toList());
            if (shang.size() > 0) {
                shangTime = Long.parseLong(shang.get(0).getBaseCheckTime());
            }
            if (xia.size() > 0) {
                xiaTime = Long.parseLong(xia.get(0).getBaseCheckTime());
            }
            obj.put("timeCount", xiaTime - shangTime);
            obj.put("count", dingAttendanceRecordShang.size());
        } else {
            obj.put("timeCount", 0);
            obj.put("count", 0);
        }
        obj.put("data", dingAttendanceRecordShang);
        return obj;
    }

    @Override
    public void generateColumn(DingAttendanceConfirmationBo dingAttendanceConfirmationBo) {

        Long loginId = SecurityFrameworkUtils.getLoginUserId();
        EmployeeDO loginEmployee = employeeMapper.selectOne(new LambdaQueryWrapper<EmployeeDO>() {{
            eq(EmployeeDO::getUserId, loginId);
        }});
        if (ObjectUtils.isEmpty(loginEmployee)) {
            throw new ResultException("当前登录账号不可以创建考勤确认单");
        }
        DingConfirmation dingConfirmation = new DingConfirmation();
        dingConfirmation.setTitle(dingAttendanceConfirmationBo.getConfirmationName());
        dingConfirmation.setTips(dingAttendanceConfirmationBo.getContent());
        dingConfirmation.setCreateBy(SecurityFrameworkUtils.getLoginUserId().intValue());
        dingConfirmation.setWorkDate(dingAttendanceConfirmationBo.getStartTime() + "至" + dingAttendanceConfirmationBo.getEndTime());
        dingConfirmation.setCreatTime(DateUtil.currentDateFormat());
        dingConfirmation.setConcat(JSONObject.toJSONString(dingAttendanceConfirmationBo));
        dingConfirmation.setStartTime(dingAttendanceConfirmationBo.getStartTime());
        dingConfirmation.setEndTime(dingAttendanceConfirmationBo.getEndTime());
        dingConfirmation.setUserId(loginEmployee.getUserId().intValue());
        dingConfirmation.setDeptId(loginEmployee.getDeptId().intValue());
        dingConfirmation.setCreateTime(LocalDateTime.now());
        dingConfirmation.setUpdateTime(LocalDateTime.now());
        dingConfirmation.setCreator("todo");
        dingConfirmation.setUpdater("todo");
        dingConfirmationMapper.insert(dingConfirmation);
        List<Integer> columnIdList = dingAttendanceConfirmationBo.getColumnIdList();
        columnIdList.forEach(columnId -> {
            ConfirmationColumn confirmationColumn = new ConfirmationColumn();
            confirmationColumn.setColumnId(columnId);
            confirmationColumn.setConfirmationId(dingConfirmation.getId());
            confirmationColumnMapper.insert(confirmationColumn);
        });
        List<Long> employeeIds = new ArrayList<>();
        if(CollUtil.isNotEmpty(dingAttendanceConfirmationBo.getEmpOffIds())){
            employeeIds = dingAttendanceConfirmationBo.getEmpOffIds();
        }

        if (!ObjectUtil.isEmpty(dingAttendanceConfirmationBo)) {
            switch (dingAttendanceConfirmationBo.getType()) {
                case 1:
                    break;
                case 2:
                    List<EmployeeDO> employeeDOS = employeeMapper.selectList(new LambdaQueryWrapper<EmployeeDO>() {{
                        in(EmployeeDO::getDeptId, dingAttendanceConfirmationBo.getDepartmentIdList());
                    }});
                    employeeIds = employeeDOS.stream().map(EmployeeDO::getId).collect(Collectors.toList());
                    break;
                case 3:
                    List<DingUser> dingUsers = dingUserMapper.selectList(new LambdaQueryWrapper<DingUser>() {{
                        in(DingUser::getTeamId, dingAttendanceConfirmationBo.getTeamIdList());
                    }});
                    employeeDOS = employeeMapper.selectList(new LambdaQueryWrapper<EmployeeDO>() {{
                        in(EmployeeDO::getDingUserId, dingUsers.stream().map(DingUser::getId).collect(Collectors.toList()));
                    }});
                    employeeIds = employeeDOS.stream().map(EmployeeDO::getId).collect(Collectors.toList());
                    break;
                case 4:
                    break;
            }
        }
        List<Long> finalEmployeeIds = employeeIds;
        List<EmployeeDO> employeeDOS = employeeMapper.selectList(new LambdaQueryWrapper<EmployeeDO>() {{
            isNotNull(EmployeeDO::getDingUserId);
            in(!ObjectUtil.isEmpty(finalEmployeeIds), EmployeeDO::getId, finalEmployeeIds);
        }});
        for (int e = 0; e < employeeDOS.size(); e++) {
            JSONObject columnObject = new JSONObject();
            EmployeeDO employeeDO1 = employeeDOS.get(e);
            DingAttendanceConfirmation dingAttendanceConfirmation = new DingAttendanceConfirmation();
            dingAttendanceConfirmation.setEmployeeId(employeeDO1.getId().intValue());
            dingAttendanceConfirmation.setDeptId(employeeDO1.getDeptId().intValue());
            dingAttendanceConfirmation.setPostId(employeeDO1.getPostId().intValue());
            dingAttendanceConfirmation.setCreateBy(SecurityFrameworkUtils.getLoginUserId().intValue());
            dingAttendanceConfirmation.setEmployeeName(employeeDO1.getName());
            dingAttendanceConfirmation.setStartTime(dingAttendanceConfirmationBo.getStartTime());
            dingAttendanceConfirmation.setEndTime(dingAttendanceConfirmationBo.getEndTime());
            EmployeeDO employeeDO = employeeDOS.get(e);
            List<DingColumnValue> dingColumnValues = dingColumnValueMapper.selectList(new QueryWrapper<DingColumnValue>() {{
                select("if (sum( column_value ) =0,column_value,sum( column_value )) AS columnValue,\n" +
                        "\tcolumn_id columnId  ");
                in("column_id", dingAttendanceConfirmationBo.getColumnIdList());
                eq("ding_user_id", employeeDO.getDingUserId());
                ge("date",dingAttendanceConfirmationBo.getStartTime());
                le("date",dingAttendanceConfirmationBo.getEndTime());
                groupBy("ding_user_id,column_id");
            }});
            for (int i = 0; i < dingColumnValues.size(); i++) {
                DingColumnValue dingColumnValue = dingColumnValues.get(i);
                if (dingColumnValue.getColumnValue().contains(".")) {
                    if (Integer.parseInt(dingColumnValue.getColumnValue().split("\\.")[1]) == 0) {
                        if (Integer.parseInt(dingColumnValue.getColumnValue().split("\\.")[0]) == 0) {
                            dingColumnValue.setColumnValue(null);
                        } else {
                            dingColumnValue.setColumnValue(dingColumnValue.getColumnValue().split("\\.")[0]);
                        }
                    }
                }
                columnObject.put(dingColumnValue.getColumnId().toString(), dingColumnValue.getColumnValue());
            }
            dingAttendanceConfirmation.setData(columnObject.toJSONString());
            dingAttendanceConfirmation.setConfirmationId(dingConfirmation.getId());
            dingAttendanceConfirmationMapper.insert(dingAttendanceConfirmation);
        }
    }

    @Override
    public void generateColumnSystem(DingAttendanceConfirmationBo dingAttendanceConfirmationBo) {
        Long loginId = SecurityFrameworkUtils.getLoginUserId();
        EmployeeDO loginEmployee = employeeMapper.selectOne(new LambdaQueryWrapper<EmployeeDO>() {{
            eq(EmployeeDO::getUserId, loginId);
        }});
        if (ObjectUtils.isEmpty(loginEmployee)) {
            throw new ResultException("当前登录账号不可以创建考勤确认单");
        }
        DateTime parse = cn.hutool.core.date.DateUtil.parse(dingAttendanceConfirmationBo.getDate(), "yyyy-MM");
        DateTime beginOfMonth = cn.hutool.core.date.DateUtil.beginOfMonth(parse);
        DateTime endOfMonth = cn.hutool.core.date.DateUtil.endOfMonth(parse);
        String beginOfMonthFormat = cn.hutool.core.date.DateUtil.format(beginOfMonth, "yyyy-MM-dd");
        String endOfMonthFormat = cn.hutool.core.date.DateUtil.format(endOfMonth, "yyyy-MM-dd");
        cn.hutool.core.date.DateUtil.format(beginOfMonth,"yyyy-MM");
        DingConfirmation dingConfirmation = new DingConfirmation();
        dingConfirmation.setTitle(dingAttendanceConfirmationBo.getConfirmationName());
        dingConfirmation.setTips(dingAttendanceConfirmationBo.getContent());
        dingConfirmation.setCreateBy(SecurityFrameworkUtils.getLoginUserId().intValue());
        dingConfirmation.setWorkDate(beginOfMonthFormat + "至" + endOfMonthFormat);
        dingConfirmation.setCreatTime(DateUtil.currentDateFormat());
        dingConfirmation.setConcat(JSONObject.toJSONString(dingAttendanceConfirmationBo));
        dingConfirmation.setStartTime(beginOfMonthFormat);
        dingConfirmation.setEndTime(endOfMonthFormat);
        dingConfirmation.setType(2);
        dingConfirmation.setUserId(loginEmployee.getUserId().intValue());
        dingConfirmation.setDeptId(loginEmployee.getDeptId().intValue());
        dingConfirmation.setCreateTime(LocalDateTime.now());
        dingConfirmation.setUpdateTime(LocalDateTime.now());
        dingConfirmation.setCreator("todo");
        dingConfirmation.setUpdater("todo");
        dingConfirmationMapper.insert(dingConfirmation);
        List<Integer> columnIdList = dingAttendanceConfirmationBo.getColumnIdList();
        columnIdList.forEach(columnId -> {
            ConfirmationColumn confirmationColumn = new ConfirmationColumn();
            confirmationColumn.setColumnId(columnId);
            confirmationColumn.setConfirmationId(dingConfirmation.getId());
            confirmationColumnMapper.insert(confirmationColumn);
        });

        List<Long> finalEmployeeIds = dingAttendanceConfirmationBo.getEmpIds();
        List<EmployeeDO> employeeDOS = employeeMapper.selectList(new LambdaQueryWrapper<EmployeeDO>() {{
            in(!ObjectUtil.isEmpty(finalEmployeeIds), EmployeeDO::getId, finalEmployeeIds);
        }});
        for (int e = 0; e < employeeDOS.size(); e++) {
            JSONObject columnObject = new JSONObject();
            EmployeeDO employeeDO1 = employeeDOS.get(e);
            DingAttendanceConfirmation dingAttendanceConfirmation = new DingAttendanceConfirmation();
            dingAttendanceConfirmation.setEmployeeId(employeeDO1.getId().intValue());
            dingAttendanceConfirmation.setDeptId(employeeDO1.getDeptId().intValue());
            dingAttendanceConfirmation.setPostId(employeeDO1.getPostId().intValue());
            dingAttendanceConfirmation.setCreateBy(SecurityFrameworkUtils.getLoginUserId().intValue());
            dingAttendanceConfirmation.setEmployeeName(employeeDO1.getName());
            dingAttendanceConfirmation.setStartTime(beginOfMonthFormat);
            dingAttendanceConfirmation.setEndTime(endOfMonthFormat);
            EmployeeDO employeeDO = employeeDOS.get(e);
            List<SystemColumnValue> systemColumnValues = systemColumnValueMapper.selectList(new QueryWrapper<SystemColumnValue>() {{
                select("if (sum( column_value ) =0,column_value,sum( column_value )) AS columnValue,\n" +
                        "\tcolumn_id columnId  ");
                in("column_id", dingAttendanceConfirmationBo.getColumnIdList());
                eq("ding_user_id", employeeDO.getId());
                eq("date",dingAttendanceConfirmationBo.getDate());
//                le("date",dingAttendanceConfirmationBo.getEndTime());
                groupBy("ding_user_id,column_id");
            }});
            for (int i = 0; i < systemColumnValues.size(); i++) {
                SystemColumnValue systemColumnValue = systemColumnValues.get(i);
                if (systemColumnValue.getColumnValue().contains(".")) {
                    if (Integer.parseInt(systemColumnValue.getColumnValue().split("\\.")[1]) == 0) {
                        if (Integer.parseInt(systemColumnValue.getColumnValue().split("\\.")[0]) == 0) {
                            systemColumnValue.setColumnValue(null);
                        } else {
                            systemColumnValue.setColumnValue(systemColumnValue.getColumnValue().split("\\.")[0]);
                        }
                    }
                }
                columnObject.put(systemColumnValue.getColumnId().toString(), systemColumnValue.getColumnValue());
            }
            dingAttendanceConfirmation.setData(columnObject.toJSONString());
            dingAttendanceConfirmation.setConfirmationId(dingConfirmation.getId());
            dingAttendanceConfirmationMapper.insert(dingAttendanceConfirmation);
        }
    }

    public PageInfo<DingAttendanceConfirmation> selectListDingAttendanceConfirmation(DingConfirmationBo dingConfirmationBo) {
        List<EmployeeDO> employeeDOS = employeeMapper.selectList(new LambdaQueryWrapper<EmployeeDO>() {{
            like(EmployeeDO::getName, dingConfirmationBo.getEmployeeName());
        }});
        PageHelper.startPage(dingConfirmationBo.getPageNum(), dingConfirmationBo.getPageSize());
        List<DingAttendanceConfirmation> dingAttendanceConfirmations = dingAttendanceConfirmationMapper.selectList(new LambdaQueryWrapper<DingAttendanceConfirmation>() {{
            eq(!ObjectUtil.isEmpty(dingConfirmationBo.getConfirmStatus()), DingAttendanceConfirmation::getConfirmStatus, dingConfirmationBo.getConfirmStatus());
            eq(!ObjectUtil.isEmpty(dingConfirmationBo.getCheckStatus()), DingAttendanceConfirmation::getCheckStatus, dingConfirmationBo.getCheckStatus());
            eq(!ObjectUtil.isEmpty(dingConfirmationBo.getSendStatus()), DingAttendanceConfirmation::getSendStatus, dingConfirmationBo.getSendStatus());
            isNotNull(null!=dingConfirmationBo.getConfirmMessageStatus() && dingConfirmationBo.getConfirmMessageStatus()  == 1, DingAttendanceConfirmation::getConfirmMessage);
            isNull(null!=dingConfirmationBo.getConfirmMessageStatus() && dingConfirmationBo.getConfirmMessageStatus()  == 0, DingAttendanceConfirmation::getConfirmMessage);
            in(!ObjectUtil.isEmpty(dingConfirmationBo.getDepartmentIds()), DingAttendanceConfirmation::getDeptId, dingConfirmationBo.getDepartmentIds());
            in(!ObjectUtil.isEmpty(employeeDOS), DingAttendanceConfirmation::getEmployeeId, employeeDOS.stream().map(EmployeeDO::getId).collect(Collectors.toList()));
            eq(DingAttendanceConfirmation::getConfirmationId, dingConfirmationBo.getConfirmId());
        }});

        if (!ObjectUtil.isEmpty(dingAttendanceConfirmations)) {
            dingAttendanceConfirmations.forEach(dingAttendanceConfirmation -> {
                EmployeeDO employeeDO = employeeMapper.selectById(dingAttendanceConfirmation.getEmployeeId());
                CommonResult<DeptRespDTO> dept = deptApi.getDept(Long.valueOf(employeeDO.getDeptId()));
                dingAttendanceConfirmation.setDeptName(dept.getData().getName());
                dingAttendanceConfirmation.setEmployeeName(employeeDO.getName());
                dingAttendanceConfirmation.setIsDimission(0);
                if(null != employeeDO.getApplySeparationTime()){
                    boolean before = employeeDO.getPlanSeparationTime().isBefore(LocalDateTime.now());
                    dingAttendanceConfirmation.setIsDimission(before?1:0);
                }
            });
        }
        return new PageInfo<>(dingAttendanceConfirmations);

    }


    @Override
    public List<JSONObject> selectByColumn() {
        List<DingColumn> dingColumns = dingColumnMapper.selectList(new LambdaQueryWrapper<DingColumn>(){{
            eq(DingColumn::getSubType, 0);
            eq(DingColumn::getStatus, 0);
        }});
        List<JSONObject> resultList = new ArrayList<>();
        dingColumns.forEach(dingColumn -> {
            JSONObject result = new JSONObject();
            result.put("id", dingColumn.getId());
            result.put("value", dingColumn.getName());
            resultList.add(result);
        });
        return resultList;
    }


    @Override
    public JSONObject selectByConfirmationColumn(Integer confirmationId) {
        List<ConfirmationColumn> confirmationColumns = confirmationColumnMapper.selectList(new LambdaQueryWrapper<ConfirmationColumn>() {{
            eq(ConfirmationColumn::getConfirmationId, confirmationId);
        }});
        List<DingColumn> dingColumns = dingColumnMapper.selectList(new LambdaQueryWrapper<DingColumn>() {{
            in(DingColumn::getId, confirmationColumns.stream().map(ConfirmationColumn::getColumnId).collect(Collectors.toList()));
        }});
        JSONObject result = new JSONObject();
        result.put("columnIdList", dingColumns.stream().map(DingColumn::getId).collect(Collectors.toList()));
        return result;


    }


    @Override
    @DataPermission(enable = false)
    public JSONObject selectById(Integer confirmationId, Integer employeeId) {
        JSONObject result = new JSONObject();
        List<ConfirmationColumn> confirmationColumns = confirmationColumnMapper.selectList(new LambdaQueryWrapper<ConfirmationColumn>() {{
            eq(ConfirmationColumn::getConfirmationId, confirmationId);
        }});
        DingConfirmation dingConfirmation = dingConfirmationMapper.selectById(confirmationId);
        List<JSONObject> resultList = new ArrayList<>();
        if(dingConfirmation.getType() == 1){
            List<DingColumn> dingColumns = dingColumnMapper.selectList(new LambdaQueryWrapper<DingColumn>() {{
                in(DingColumn::getId, confirmationColumns.stream().map(ConfirmationColumn::getColumnId).collect(Collectors.toList()));
            }});
            dingColumns.forEach(dingColumn -> {
                JSONObject columnResult = new JSONObject();
                columnResult.put("id", dingColumn.getId());
                columnResult.put("value", dingColumn.getName());
                columnResult.put("isBuilt",1);
                resultList.add(columnResult);
            });
        }else{
            List<SystemColumnTenant> systemColumnTenantList = systemColumnTenantMapper.selectList(new LambdaQueryWrapper<SystemColumnTenant>() {{
                in(SystemColumnTenant::getId, confirmationColumns.stream().map(ConfirmationColumn::getColumnId).collect(Collectors.toList()));
            }});
            systemColumnTenantList.forEach(systemColumn -> {
                JSONObject columnResult = new JSONObject();
                columnResult.put("id", systemColumn.getId());
                columnResult.put("value", systemColumn.getName());
                columnResult.put("isBuilt",systemColumn.getIsBuilt());
                resultList.add(columnResult);
            });
        }
//        EmployeeDO employeeDO = employeeMapper.selectOne(new LambdaQueryWrapper<EmployeeDO>() {{
//            eq(EmployeeDO::getUserId, employeeId);
//        }});
        DingAttendanceConfirmation dingAttendanceConfirmation = dingAttendanceConfirmationMapper.selectOne(new LambdaQueryWrapper<DingAttendanceConfirmation>() {{
            eq(DingAttendanceConfirmation::getConfirmationId, confirmationId);
            eq(DingAttendanceConfirmation::getEmployeeId, employeeId);
            last("limit 1");
        }});
        String workDate = dingAttendanceConfirmation.getData();
        CommonResult<DeptRespDTO> dept = deptApi.getDept(Long.valueOf(dingAttendanceConfirmation.getDeptId()));
        result.put("id", dingAttendanceConfirmation.getId());
        result.put("name", "考勤确认单");
        result.put("title", dingConfirmation.getTitle());
        result.put("tips", dingConfirmation.getTips());
        result.put("deptName", dept.getCheckedData().getName());
        result.put("employeeName", dingAttendanceConfirmation.getEmployeeName());
        result.put("columnValue", JSONObject.parseObject(workDate));
        result.put("column", resultList);
        return result;
    }

    @Override
    @DataPermission(enable = false)
    public JSONObject selectEmployeeById(Integer confirmationId, Integer employeeId) {
        JSONObject result = new JSONObject();
        List<ConfirmationColumn> confirmationColumns = confirmationColumnMapper.selectList(new LambdaQueryWrapper<ConfirmationColumn>() {{
            eq(ConfirmationColumn::getConfirmationId, confirmationId);
        }});
        DingConfirmation dingConfirmation = dingConfirmationMapper.selectById(confirmationId);
        List<JSONObject> resultList = new ArrayList<>();
        if(dingConfirmation.getType() == 1){
            List<DingColumn> dingColumns = dingColumnMapper.selectList(new LambdaQueryWrapper<DingColumn>() {{
                in(DingColumn::getId, confirmationColumns.stream().map(ConfirmationColumn::getColumnId).collect(Collectors.toList()));
            }});
            dingColumns.forEach(dingColumn -> {
                JSONObject columnResult = new JSONObject();
                columnResult.put("id", dingColumn.getId());
                columnResult.put("value", dingColumn.getName());
                resultList.add(columnResult);
            });
        }else{
            List<SystemColumnTenant> systemColumnTenantList = systemColumnTenantMapper.selectList(new LambdaQueryWrapper<SystemColumnTenant>() {{
                in(SystemColumnTenant::getId, confirmationColumns.stream().map(ConfirmationColumn::getColumnId).collect(Collectors.toList()));
            }});
            systemColumnTenantList.forEach(systemColumn -> {
                JSONObject columnResult = new JSONObject();
                columnResult.put("id", systemColumn.getId());
                columnResult.put("value", systemColumn.getName());
                resultList.add(columnResult);
            });
        }
        EmployeeDO employeeDO = employeeMapper.selectOne(new LambdaQueryWrapper<EmployeeDO>() {{
            eq(EmployeeDO::getUserId, employeeId);
        }});
//      DingConfirmation dingConfirmation = dingConfirmationMapper.selectById(confirmationId);
        DingAttendanceConfirmation dingAttendanceConfirmation = dingAttendanceConfirmationMapper.selectOne(new LambdaQueryWrapper<DingAttendanceConfirmation>() {{
            eq(DingAttendanceConfirmation::getConfirmationId, confirmationId);
            eq(DingAttendanceConfirmation::getEmployeeId, employeeDO.getId());
            last("limit 1");
        }});
        String workDate = dingAttendanceConfirmation.getData();
        CommonResult<DeptRespDTO> dept = deptApi.getDept(Long.valueOf(dingAttendanceConfirmation.getDeptId()));
        result.put("id", dingAttendanceConfirmation.getId());
        result.put("name", "考勤确认单");
        result.put("title", dingConfirmation.getTitle());
        result.put("tips", dingConfirmation.getTips());
        result.put("deptName", dept.getCheckedData().getName());
        result.put("employeeName", dingAttendanceConfirmation.getEmployeeName());
        result.put("columnValue", JSONObject.parseObject(workDate));
        result.put("column", resultList);
        return result;
    }


    @Override
//    @DataPermission(enable = false)
    public List<DingConfirmation> selectByConfirmation(String workDate) {
        List<DingConfirmation> dingConfirmationList = dingConfirmationMapper.selectList(new LambdaQueryWrapper<DingConfirmation>() {{
            le(DingConfirmation::getStartTime, workDate + "-31 00:00:00");
            ge(DingConfirmation::getEndTime, workDate + "-01 00:00:00");
            orderByDesc(DingConfirmation::getCreatTime);

        }});
        dingConfirmationList.forEach(dingConfirmation -> {
            Long sendCount = dingAttendanceConfirmationMapper.selectCount(
                    new LambdaQueryWrapper<DingAttendanceConfirmation>().eq(DingAttendanceConfirmation::getConfirmationId, dingConfirmation.getId())
                            .eq(DingAttendanceConfirmation::getSendStatus, 1)

            );

            Long checkCount = dingAttendanceConfirmationMapper.selectCount(
                    new LambdaQueryWrapper<DingAttendanceConfirmation>().eq(DingAttendanceConfirmation::getConfirmationId, dingConfirmation.getId())
                            .eq(DingAttendanceConfirmation::getCheckStatus, 1)

            );
            Long confirmCount = dingAttendanceConfirmationMapper.selectCount(
                    new LambdaQueryWrapper<DingAttendanceConfirmation>().eq(DingAttendanceConfirmation::getConfirmationId, dingConfirmation.getId())
                            .eq(DingAttendanceConfirmation::getConfirmStatus, 1)

            );
            Long sumCount = dingAttendanceConfirmationMapper.selectCount(
                    new LambdaQueryWrapper<DingAttendanceConfirmation>().eq(DingAttendanceConfirmation::getConfirmationId, dingConfirmation.getId())

            );
            dingConfirmation.setCheckCount(checkCount);
            dingConfirmation.setSendCount(sendCount);
            dingConfirmation.setConfirmCount(confirmCount);
            dingConfirmation.setSumCount(sumCount);
            EmployeeDO employeeDO = employeeMapper.selectByUserId(dingConfirmation.getCreateBy().longValue());
            if (!ObjectUtil.isEmpty(employeeDO)) {
                dingConfirmation.setCreateByName(employeeDO.getName());
            }
        });
        return dingConfirmationList;
    }

    @Override
    @DataPermission(enable = false)
    public DingConfirmation selectByConfirmationOne(Integer confirmationId) {
        DingConfirmation dingConfirmation = dingConfirmationMapper.selectById(confirmationId);

        Long sendCount = dingAttendanceConfirmationMapper.selectCount(
                new LambdaQueryWrapper<DingAttendanceConfirmation>().eq(DingAttendanceConfirmation::getConfirmationId, dingConfirmation.getId())
                        .eq(DingAttendanceConfirmation::getSendStatus, 1)

        );

        Long checkCount = dingAttendanceConfirmationMapper.selectCount(
                new LambdaQueryWrapper<DingAttendanceConfirmation>().eq(DingAttendanceConfirmation::getConfirmationId, dingConfirmation.getId())
                        .eq(DingAttendanceConfirmation::getCheckStatus, 1)

        );
        Long confirmCount = dingAttendanceConfirmationMapper.selectCount(
                new LambdaQueryWrapper<DingAttendanceConfirmation>().eq(DingAttendanceConfirmation::getConfirmationId, dingConfirmation.getId())
                        .eq(DingAttendanceConfirmation::getConfirmStatus, 1)

        );
        Long sumCount = dingAttendanceConfirmationMapper.selectCount(
                new LambdaQueryWrapper<DingAttendanceConfirmation>().eq(DingAttendanceConfirmation::getConfirmationId, dingConfirmation.getId())

        );
        EmployeeDO employeeDO = employeeMapper.selectByUserId(dingConfirmation.getCreateBy().longValue());
        if(!ObjectUtil.isEmpty(employeeDO)){
            dingConfirmation.setCreateByName(employeeDO.getName());
        }
        dingConfirmation.setCheckCount(checkCount);
        dingConfirmation.setSendCount(sendCount);
        dingConfirmation.setConfirmCount(confirmCount);
        dingConfirmation.setSumCount(sumCount);
        return dingConfirmation;
    }

    @Override
    @DataPermission(enable = false)
    public Integer UpdateGenerateColumn(DingAttendanceConfirmationBo dingAttendanceConfirmationBo) {
        dingConfirmationMapper.deleteById(dingAttendanceConfirmationBo.getConfirmationId());
        dingAttendanceConfirmationMapper.delete(new LambdaQueryWrapper<DingAttendanceConfirmation>() {{
            eq(DingAttendanceConfirmation::getConfirmationId, dingAttendanceConfirmationBo.getConfirmationId());
        }});
        confirmationColumnMapper.delete(new LambdaQueryWrapper<ConfirmationColumn>() {{
            eq(ConfirmationColumn::getConfirmationId, dingAttendanceConfirmationBo.getConfirmationId());
        }});
        generateColumn(dingAttendanceConfirmationBo);
        return 0;
    }

    @Override
    @DataPermission(enable = false)
    public Integer removeConfirmation(Integer id) {
        DingConfirmation dingConfirmation = dingConfirmationMapper.selectById(id);
        dingAttendanceConfirmationMapper.delete(new LambdaQueryWrapper<DingAttendanceConfirmation>() {{
            eq(DingAttendanceConfirmation::getConfirmationId, id);
        }});
        if(dingConfirmation.getType() == 1){
            confirmationColumnMapper.delete(new LambdaQueryWrapper<ConfirmationColumn>() {{
                eq(ConfirmationColumn::getConfirmationId, id);
            }});
        }else{
            String date = cn.hutool.core.date.DateUtil.format(cn.hutool.core.date.DateUtil.parse(dingConfirmation.getStartTime(),"yyyy-MM-dd"),"yyyy-MM");
            cn.hutool.json.JSONObject object = new cn.hutool.json.JSONObject(dingConfirmation.getConcat());
            JSONArray empIds = object.getJSONArray("empIds");
            if(null != empIds && empIds.size() > 0){
                systemColumnValueMapper.delete(new LambdaQueryWrapper<SystemColumnValue>() {{
                    in(SystemColumnValue::getDingUserId, empIds);
                    eq(SystemColumnValue::getDate,date);
                }});
            }
        }


        dingConfirmationMapper.deleteById(id);
        return 0;
    }

    @Override
    @DataPermission(enable = false)
    public JSONObject selectUpdateConfirmationById(Integer id) {
        DingConfirmation dingConfirmation = dingConfirmationMapper.selectById(id);

        return JSONObject.parseObject(dingConfirmation.getConcat());
    }

    @Override
    public Integer updateConfirmationInfo(JSONObject param) {
//        String string = param.getString("employeeName");
        JSONObject jsonObject = param.getJSONObject("columnValue");
        Integer integer = param.getInteger("id");
        DingAttendanceConfirmation dingAttendanceConfirmation = dingAttendanceConfirmationMapper.selectById(integer);
//        dingAttendanceConfirmation.setEmployeeName(string);
        dingAttendanceConfirmation.setData(jsonObject.toJSONString());
        dingAttendanceConfirmationMapper.updateById(dingAttendanceConfirmation);

        Integer employeeId = dingAttendanceConfirmation.getEmployeeId();

        String time = DateUtil.stringDateToMouth(dingAttendanceConfirmation.getStartTime());
        List<SystemColumnValue> systemColumnValues = systemColumnValueMapper.selectList(new LambdaQueryWrapper<SystemColumnValue>() {{
            eq(SystemColumnValue::getDingUserId, employeeId);
            eq(SystemColumnValue::getDate, time);
        }});
        systemColumnValues.forEach(systemColumnValue -> {
            String string = jsonObject.getString(systemColumnValue.getColumnId() + "");
            systemColumnValue.setColumnValue(string);
            systemColumnValueMapper.updateById(systemColumnValue);



        });



        return 0;
    }

    public static void main(String[] args) {

        System.err.println(DateUtil.stringDateToMouth("2025-05-01"));
    }

    @Override
    public Integer sendAllConfirmation(Integer id) {
        List<DingAttendanceConfirmation> dingAttendanceConfirmationList = dingAttendanceConfirmationMapper.selectList(new LambdaQueryWrapper<DingAttendanceConfirmation>() {{
            eq(DingAttendanceConfirmation::getConfirmationId, id);
        }});
        dingAttendanceConfirmationList.forEach(dingAttendanceConfirmation -> {
            dingAttendanceConfirmation.setSendStatus(1);
            dingAttendanceConfirmation.setSendTime(DateUtil.currentDateFormat());
            dingAttendanceConfirmationMapper.updateById(dingAttendanceConfirmation);
        });

        return 0;
    }

    @Override
    public Integer sendConfirmation(Integer id) {
        DingAttendanceConfirmation dingAttendanceConfirmation = dingAttendanceConfirmationMapper.selectById(id);
        dingAttendanceConfirmation.setSendStatus(1);
        dingAttendanceConfirmation.setSendTime(DateUtil.currentDateFormat());
        dingAttendanceConfirmation.setConfirmMessage("");
        dingAttendanceConfirmationMapper.updateById(dingAttendanceConfirmation);
        return 0;
    }

    @Override
    public Integer withdrawConfirmation(Integer id) {
        DingAttendanceConfirmation dingAttendanceConfirmation = dingAttendanceConfirmationMapper.selectById(id);
        dingAttendanceConfirmation.setSendStatus(2);
        dingAttendanceConfirmation.setConfirmStatus(0);
        dingAttendanceConfirmation.setCheckStatus(0);
        dingAttendanceConfirmation.setSendTime(DateUtil.currentDateFormat());
        dingAttendanceConfirmationMapper.updateById(dingAttendanceConfirmation);
        return 0;
    }

    @Override
    public Integer withdrawAllConfirmation(Integer id) {
        List<DingAttendanceConfirmation> dingAttendanceConfirmationList = dingAttendanceConfirmationMapper.selectList(new LambdaQueryWrapper<DingAttendanceConfirmation>() {{
            eq(DingAttendanceConfirmation::getConfirmationId, id);
        }});
        dingAttendanceConfirmationList.forEach(dingAttendanceConfirmation -> {
            dingAttendanceConfirmation.setSendStatus(2);
            dingAttendanceConfirmation.setSendTime(DateUtil.currentDateFormat());
            dingAttendanceConfirmationMapper.updateById(dingAttendanceConfirmation);
        });

        return 0;
    }

    @Override
    public Integer checkConfirmation(Integer id) {
        DingAttendanceConfirmation dingAttendanceConfirmation = dingAttendanceConfirmationMapper.selectById(id);
        dingAttendanceConfirmation.setCheckStatus(1);
        dingAttendanceConfirmation.setCheckTime(DateUtil.currentDateFormat());
        dingAttendanceConfirmationMapper.updateById(dingAttendanceConfirmation);
        return 0;
    }

    @Override
    public Integer confirmConfirmation(Integer id, String message, Integer status) {

        DingAttendanceConfirmation dingAttendanceConfirmation = dingAttendanceConfirmationMapper.selectById(id);
        dingAttendanceConfirmation.setConfirmStatus(status);
        if (status == 1) {
            dingAttendanceConfirmation.setConfirmMessage("");
        } else {
            dingAttendanceConfirmation.setConfirmMessage(message);

        }
        dingAttendanceConfirmation.setConfirmTime(DateUtil.currentDateFormat());
        dingAttendanceConfirmationMapper.updateById(dingAttendanceConfirmation);
        return 0;
    }

    /**
     * 员工确认回显
     *
     * @param employeeId
     * @return
     */
    @Override
    @DataPermission(enable = false)
    public PageInfo selectConfirmationByEmployee(Integer employeeId, Integer pageNum, Integer pageSize) {

        EmployeeDO employeeDO = employeeMapper.selectOne(new LambdaQueryWrapper<EmployeeDO>() {{
            eq(EmployeeDO::getUserId, employeeId);
        }});

        PageHelper.startPage(pageNum, pageSize);
        List<JSONObject> jsonObjects = dingAttendanceConfirmationMapper.selectOList(employeeDO.getId().intValue());

        for (int i = 0; i < jsonObjects.size(); i++) {
            Integer confirmationId = jsonObjects.get(i).getInteger("confirmationId");
            DingConfirmation dingConfirmation = dingConfirmationMapper.selectById(confirmationId);
            jsonObjects.get(i).put("confirmCycle", dingConfirmation.getWorkDate());
            if (jsonObjects.get(i).getInteger("confirmStatus") == 0) {
                jsonObjects.get(i).put("confirmStatus", 0);
            } else if (jsonObjects.get(i).getInteger("checkStatus") == 1 && jsonObjects.get(i).getInteger("confirmStatus") == 1) {
                jsonObjects.get(i).put("confirmStatus", 1);
            }
        }
        return new PageInfo<>(jsonObjects);

    }

    @Override
    @DataPermission(enable = false)
    public Map<String, List<List<String>>> selectConfirmExcel(Integer confirmationId) {
        DingConfirmation dingConfirmation = dingConfirmationMapper.selectById(confirmationId);

        Map<String, List<List<String>>> result = new HashMap<>();
        List<ConfirmationColumn> confirmationColumns = confirmationColumnMapper.selectList(new LambdaQueryWrapper<ConfirmationColumn>() {{
            eq(ConfirmationColumn::getConfirmationId, confirmationId);
        }});
        List<List<String>> headList = new ArrayList<>();
        List<String> header = new ArrayList<>();
        header.add("姓名");
        headList.add(header);
        header = new ArrayList<>();
        header.add("部门");
        headList.add(header);
        header = new ArrayList<>();
        header.add("发送状态");
        headList.add(header);
        header = new ArrayList<>();
        header.add("发送时间");
        headList.add(header);
        header = new ArrayList<>();
        header.add("撤回时间");
        headList.add(header);
        header = new ArrayList<>();
        header.add("查看状态");
        headList.add(header);
        header = new ArrayList<>();
        header.add("查看时间");
        headList.add(header);
        header = new ArrayList<>();
        header.add("确认状态");
        headList.add(header);
        header = new ArrayList<>();
        header.add("确认时间");
        headList.add(header);
        // 1. 钉钉拉取  2. 系统导入
        Integer type = dingConfirmation.getType();
        for (int i = 0; i < confirmationColumns.size(); i++) {
            if(type == 1){
                DingColumn dingColumn = dingColumnMapper.selectById(confirmationColumns.get(i).getColumnId());
                List<String> Columnheader = new ArrayList<>();
                Columnheader.add(dingColumn.getName());
                headList.add(Columnheader);
            }else{
                SystemColumnTenant dingColumn = systemColumnTenantMapper.selectById(confirmationColumns.get(i).getColumnId());
                List<String> Columnheader = new ArrayList<>();
                Columnheader.add(dingColumn.getName());
                headList.add(Columnheader);
            }

        }
        List<List<String>> data = new ArrayList<>();
        List<DingAttendanceConfirmation> dingAttendanceConfirmationList = dingAttendanceConfirmationMapper.selectList(new LambdaQueryWrapper<DingAttendanceConfirmation>() {{
            eq(DingAttendanceConfirmation::getConfirmationId, confirmationId);
        }});
        dingAttendanceConfirmationList.forEach(dingAttendanceConfirmation -> {
            String sendStatus = "";
            String sendTime = "";
            String returnTime = "";
            String checkStatus = "";
            String checkTime = "";
            String confirmStatus = "";
            String confirmTime = "";
            if (dingAttendanceConfirmation.getSendStatus() == 0) {
                sendStatus = "未发送";

            } else if (dingAttendanceConfirmation.getSendStatus() == 1) {
                sendStatus = "已发送";
                sendTime = dingAttendanceConfirmation.getSendTime();
            } else {
                returnTime = dingAttendanceConfirmation.getSendTime();
                sendStatus = "已撤回";
            }

            if (dingAttendanceConfirmation.getCheckStatus() == 0) {
                checkStatus = "未查看";
            } else {
                checkStatus = "已查看";
                checkTime = dingAttendanceConfirmation.getCheckTime();
            }

            if (dingAttendanceConfirmation.getConfirmStatus() == 0) {
                checkStatus = "未确认";
            } else {
                checkStatus = "已确认";
                checkTime = dingAttendanceConfirmation.getConfirmTime();
            }
            List<String> dataColumn = new ArrayList<>();
            String workDate = dingAttendanceConfirmation.getData();
            CommonResult<DeptRespDTO> dept = deptApi.getDept(Long.valueOf(dingAttendanceConfirmation.getDeptId()));
            JSONObject jsonObject = JSONObject.parseObject(workDate);
            dataColumn.add(dingAttendanceConfirmation.getEmployeeName());
            dataColumn.add(dept.getCheckedData().getName());
            dataColumn.add(sendStatus);
            dataColumn.add(sendTime);
            dataColumn.add(returnTime);
            dataColumn.add(checkStatus);
            dataColumn.add(checkTime);
            dataColumn.add(confirmStatus);
            dataColumn.add(confirmTime);
            for (int i = 0; i < confirmationColumns.size(); i++) {
                dataColumn.add(jsonObject.getString(confirmationColumns.get(i).getColumnId() + ""));
            }
            data.add(dataColumn);

        });
        result.put("header", headList);
        result.put("data", data);

        System.err.println(result.toString());
        return result;
    }

    @Override
    public Map<String, List<List<String>>> selectColumnMonthExcel(DingAttendanceRecordBo dingAttendanceRecordBo) {
        Map<String, List<List<String>>> result = new HashMap<>();
        List<List<String>> headList = new ArrayList<>();
        if (!ObjectUtil.isEmpty(dingAttendanceRecordBo)) {
            switch (dingAttendanceRecordBo.getType()) {
                case 1:
                    dingAttendanceRecordBo.setEmployeeIdList(new ArrayList<>());
                    break;
                case 2:
                    break;
                case 3:
                    break;
                case 4:
                    break;
            }
        }
        List<String> strDateListBetween = DateUtil.getStrDateListBetween(dingAttendanceRecordBo.getStartTime(), dingAttendanceRecordBo.getEndTime());
        List<DingColumn> dingColumns = dingColumnMapper.selectList(new LambdaQueryWrapper<DingColumn>() {{
            eq(DingColumn::getSubType, 0);
            eq(DingColumn::getStatus, 0);
        }});
        List<String> header = new ArrayList<>();
        header.add("姓名");
        headList.add(header);
        header = new ArrayList<>();
        header.add("考勤组");
        headList.add(header);
        header = new ArrayList<>();
        header.add("部门");
        headList.add(header);
        header = new ArrayList<>();
        header.add("工号");
        headList.add(header);
        header = new ArrayList<>();
        header.add("职位");
        headList.add(header);
        for (int i = 0; i < dingColumns.size(); i++) {
            header = new ArrayList<>();
            DingColumn dingColumn = dingColumns.get(i);
            header.add(dingColumn.getName());
            headList.add(header);
        }
        for (int i = 0; i < strDateListBetween.size(); i++) {
            header = new ArrayList<>();
            header.add(strDateListBetween.get(i));
            headList.add(header);
        }
        List<EmployeeDO> employeeDOS = employeeMapper.selectList(new LambdaQueryWrapper<EmployeeDO>() {{
            isNotNull(EmployeeDO::getDingUserId);
            in(!ObjectUtil.isEmpty(dingAttendanceRecordBo.getEmployeeIdList()), EmployeeDO::getUserId, dingAttendanceRecordBo.getEmployeeIdList());
        }});
        List<List<String>> valuesObject = new ArrayList<>();
        for (int e = 0; e < employeeDOS.size(); e++) {
            EmployeeDO employeeDO = employeeDOS.get(e);
            List<String> dataColumn = new ArrayList<>();
            dataColumn.add(employeeDO.getName());
            DingUser dingUser = dingUserMapper.selectById(employeeDO.getDingUserId());
            if (!ObjectUtil.isEmpty(dingUser)) {
                DingAttendanceTeam dingAttendanceTeam = dingAttendanceTeamMapper.selectById(dingUser.getTeamId());
                if (!ObjectUtil.isEmpty(dingAttendanceTeam)) {
                    dataColumn.add(dingAttendanceTeam.getName());
                }
            }
            CommonResult<DeptRespDTO> dept = deptApi.getDept(employeeDO.getDeptId());
            dataColumn.add(dept.getCheckedData().getName());
            dataColumn.add(employeeDO.getEmployeeNo());
            CommonResult<PostRespDTO> post = postApi.getPost(employeeDO.getPostId());
            dataColumn.add(post.getCheckedData().getName());
            for (int i = 0; i < dingColumns.size(); i++) {
                int finalI = i;
                DingColumnValue dingColumnValue = dingColumnValueMapper.selectOne(new QueryWrapper<DingColumnValue>() {{
                select("if (sum( column_value ) =0,column_value,sum( column_value )) AS columnValue,\n" +
                        "\tcolumn_id columnId  ");
                eq("ding_user_id", employeeDO.getDingUserId());
                le("date", dingAttendanceRecordBo.getEndTime() + " 23:59:59");
                ge("date", dingAttendanceRecordBo.getStartTime() + " 00:00:00");
               eq("column_id",dingColumns.get(finalI).getId());
            }});
                if(ObjectUtils.isEmpty(dingColumnValue.getColumnValue())||"0".equals(dingColumnValue.getColumnValue())||"0.0".equals(dingColumnValue.getColumnValue())){
                    dataColumn.add("");
                }else {
                    dataColumn.add(dingColumnValue.getColumnValue());
                }
            }

            for (int i = 0; i < strDateListBetween.size(); i++) {
                Integer dingUserId = employeeDO.getDingUserId();
                DingUser dingUserZ = dingUserMapper.selectById(dingUserId);
                Long l = DateUtil.strZLong(strDateListBetween.get(i));
                DingAttendanceRecord dingAttendanceRecordShang = dingAttendanceRecordMapper.selectOne(new LambdaQueryWrapper<DingAttendanceRecord>() {{
                    eq(DingAttendanceRecord::getUserId, dingUserZ.getUserid());
                    eq(DingAttendanceRecord::getWorkDate, l);
                    eq(DingAttendanceRecord::getCheckType, 1);
                    last("limit 1");
                }});
                DingAttendanceRecord dingAttendanceRecordXia = dingAttendanceRecordMapper.selectOne(new LambdaQueryWrapper<DingAttendanceRecord>() {{
                    eq(DingAttendanceRecord::getUserId, dingUserZ.getUserid());
                    eq(DingAttendanceRecord::getWorkDate, l);
                    eq(DingAttendanceRecord::getCheckType, 2);
                    last("limit 1");
                }});

                List<JSONObject> status = new ArrayList<>();
                if (!ObjectUtil.isEmpty(dingAttendanceRecordShang) && !ObjectUtil.isEmpty(dingAttendanceRecordXia)) {
                    JSONObject obj = new JSONObject();
                    if (dingAttendanceRecordShang.getTimeResult() == 1 && dingAttendanceRecordXia.getTimeResult() == 1) {
                        dataColumn.add("正常");
                        obj.put("type", 1);
                        status.add(obj);
                    } else if (dingAttendanceRecordShang.getTimeResult() == 1) {
                        switch (dingAttendanceRecordXia.getTimeResult()) {
                            case 2:

                                dataColumn.add("下班早退");
                                obj.put("type", 2);
                                status.add(obj);
                                break;

                            case 6:
                                dataColumn.add("下班未打卡");
                                obj.put("type", 2);
                                status.add(obj);

                                break;
                        }
                    } else if (dingAttendanceRecordXia.getTimeResult() == 1) {
                        switch (dingAttendanceRecordShang.getTimeResult()) {
                            case 3:
                                dataColumn.add("迟到");
                                obj.put("type", 2);
                                status.add(obj);
                                break;

                            case 4:
                                dataColumn.add("严重迟到");
                                obj.put("type", 2);
                                status.add(obj);
                                break;
                            case 5:
                                dataColumn.add("旷工迟到");
                                obj.put("type", 2);
                                status.add(obj);
                                break;
                            case 6:
                                dataColumn.add("未打卡");
                                obj.put("type", 2);
                                status.add(obj);
                                break;
                        }
                    } else if (dingAttendanceRecordShang.getTimeResult() == 6 && dingAttendanceRecordXia.getTimeResult() == 6) {
                        dataColumn.add("旷工");
                        obj.put("type", 2);
                        status.add(obj);
                    } else {
                        switch (dingAttendanceRecordXia.getTimeResult()) {
                            case 2:
                                dataColumn.add("下班早退");
                                obj.put("type", 2);
                                status.add(obj);
                                break;

                            case 6:
                                dataColumn.add("下班未打卡");
                                obj.put("type", 2);
                                status.add(obj);
                                break;
                        }
                        switch (dingAttendanceRecordShang.getTimeResult()) {
                            case 3:
                                dataColumn.add("迟到");
                                obj.put("type", 2);
                                status.add(obj);
                                break;

                            case 4:
                                dataColumn.add("严重迟到");
                                obj.put("type", 2);
                                status.add(obj);
                                break;
                            case 5:
                                dataColumn.add("旷工迟到");
                                obj.put("type", 2);
                                status.add(obj);
                                break;
                            case 6:
                                dataColumn.add("上班未打卡");
                                obj.put("type", 2);
                                status.add(obj);
                                break;
                        }
                    }
                }else {
                    JSONObject obj = new JSONObject();
                    dataColumn.add("");
                    obj.put("type", 2);
                    status.add(obj);
                }
            }
            valuesObject.add(dataColumn);
        }
        result.put("header", headList);
        result.put("data", valuesObject);
        return result;
    }

    @Override
    public String cropInfo() {
        DingTenant dingTenant = dingTenantMapper.selectOne(null);
        if (!ObjectUtil.isEmpty(dingTenant)) {
            return dingTenant.getTriggerName();
        }
        return "";
    }

    @Override
    @TenantIgnore
    public Integer bangDingCrop(String cropName, Long headerTenantId) {
        DingTenant dingTenant = dingTenantMapper.selectOne(new LambdaQueryWrapper<DingTenant>() {{
            eq(DingTenant::getTriggerName, cropName);
        }});
        if (ObjectUtil.isEmpty(dingTenant)) {
            return -1;
        } else {
            dingTenant.setTenantId(headerTenantId.intValue());
            dingTenantMapper.updateById(dingTenant);
            return 0;
        }


    }

    @Override
    public Map<String, List<List<String>>> selectColumnExcel(DingAttendanceRecordBo dingAttendanceRecordBo) {
        Map<String, List<List<String>>> result = new HashMap<>();
        if (!ObjectUtil.isEmpty(dingAttendanceRecordBo)) {
            switch (dingAttendanceRecordBo.getType()) {
                case 1:
                    dingAttendanceRecordBo.setEmployeeIdList(new ArrayList<>());
                    break;
                case 2:
                    break;
                case 3:
                    break;
                case 4:
                    break;
            }
        }
        List<List<String>> headList = new ArrayList<>();
        List<JSONObject> columnObjectList = new ArrayList<>();
        List<DingColumn> dingColumns = dingColumnMapper.selectList(new LambdaQueryWrapper<DingColumn>() {{
            eq(DingColumn::getStatus, 0);
        }});
        List<String> header = new ArrayList<>();
        header.add("姓名");
        headList.add(header);
        header = new ArrayList<>();
        header.add("考勤组");
        headList.add(header);
        header = new ArrayList<>();
        header.add("部门");
        headList.add(header);
        header = new ArrayList<>();
        header.add("工号");
        headList.add(header);
        header = new ArrayList<>();
        header.add("职位");
        headList.add(header);
        for (int i = 0; i < dingColumns.size(); i++) {
            header = new ArrayList<>();
            DingColumn dingColumn = dingColumns.get(i);
            header.add(dingColumn.getName());
            headList.add(header);
        }

        List<List<String>> data = new ArrayList<>();
        List<EmployeeDO> employeeDOS = employeeMapper.selectList(new LambdaQueryWrapper<EmployeeDO>() {{
            isNotNull(EmployeeDO::getDingUserId);
            in(!ObjectUtil.isEmpty(dingAttendanceRecordBo.getEmployeeIdList()), EmployeeDO::getId, dingAttendanceRecordBo.getEmployeeIdList());
        }});
        List<JSONObject> valuesObject = new ArrayList<>();

        for (int e = 0; e < employeeDOS.size(); e++) {
            List<String> dataColumn = new ArrayList<>();
            EmployeeDO employeeDO = employeeDOS.get(e);
            List<DingColumnValue> dingColumnValues = dingColumnValueMapper.selectList(new LambdaQueryWrapper<DingColumnValue>() {{
                eq(DingColumnValue::getDingUserId, employeeDO.getDingUserId());
            }});
            JSONObject columnValueObject = new JSONObject();
            dataColumn.add(employeeDO.getName());
            DingUser dingUser = dingUserMapper.selectById(employeeDO.getDingUserId());
            if (!ObjectUtil.isEmpty(dingUser)) {
                DingAttendanceTeam dingAttendanceTeam = dingAttendanceTeamMapper.selectById(dingUser.getTeamId());
                if (!ObjectUtil.isEmpty(dingAttendanceTeam)) {
                    dataColumn.add(dingAttendanceTeam.getName());
                }
            }
            CommonResult<DeptRespDTO> dept = deptApi.getDept(employeeDO.getDeptId());
            dataColumn.add(dept.getCheckedData().getName());
            dataColumn.add(employeeDO.getEmployeeNo());
            CommonResult<PostRespDTO> post = postApi.getPost(employeeDO.getPostId());
            dataColumn.add(post.getCheckedData().getName());
            for (int i = 0; i < dingColumnValues.size(); i++) {
                DingColumnValue dingColumnValue = dingColumnValues.get(i);
                if (dingColumnValue.getColumnValue().contains(".")) {
                    if (Integer.parseInt(dingColumnValue.getColumnValue().split("\\.")[1]) == 0) {
                        if (Integer.parseInt(dingColumnValue.getColumnValue().split("\\.")[0]) == 0) {
                            dingColumnValue.setColumnValue("");
                        } else {
                            dingColumnValue.setColumnValue(dingColumnValue.getColumnValue().split("\\.")[0]);
                        }
                    }
                }
                if (dingColumnValue.getColumnId() == 1 || dingColumnValue.getColumnId() == 3 || dingColumnValue.getColumnId() == 5 || dingColumnValue.getColumnId() == 7 || dingColumnValue.getColumnId() == 9 || dingColumnValue.getColumnId() == 11) {
                    JSONObject object = new JSONObject();
                    object.put("id", dingColumnValue.getId());
                    if (dingColumnValue.getColumnValue().equals("正常")) {
                        object.put("type", 1);
                    } else {
                        object.put("type", 2);
                    }
                    dataColumn.add(dingColumnValue.getColumnValue());
                    columnValueObject.put(dingColumnValue.getColumnId().toString(), object);
                } else {
                    dataColumn.add(dingColumnValue.getColumnValue());

                }
            }
            data.add(dataColumn);
            valuesObject.add(columnValueObject);
        }
        result.put("header", headList);
        result.put("data", data);

        return result;
    }



}


//    public Boolean isInteger(String str) {
//
//        Pattern pattern = Pa